<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;;
use think\Db;
use think\Loader;

class Tbb extends AdminBase
{
    protected $qi = [
        '不挂旗' => 0,
        '红旗' => 1,
        '黄旗' => 2,
        '黑旗' => 3
    ];

    protected function _initialize()
    {
        parent::_initialize();
    }

public function index()
{
    $limit = input('limit', 20);
    $page = input('page', 1);
    $record = Db::name('list')
        ->alias('l')
        ->join('bd_area p', 'l.province = p.id', 'LEFT')
        ->join('bd_area c', 'l.city = c.id', 'LEFT')
        ->join('bd_area d', 'l.district = d.id', 'LEFT')
        ->field('l.*, p.name as province_name, c.name as city_name, d.name as district_name')
        ->order('l.id', 'desc')
        ->paginate($limit, false, ['page' => $page]);
    $recordList = [];
    foreach ($record as $k => $v) {
        $files = array_filter(explode(';', $v['files'])); // 将文件路径分割成数组并过滤空值

        $recordList[] = [
            'id' => $v['id'],
            'name' => $v['name'],
            'idcard' => $v['idcard'],
            'contact' => $v['contact'],
            'province' => $v['province_name'],
            'city' => $v['city_name'],
            'district' => $v['district_name'],
            'file' => $v['file'],
            'files' => $files,
            'create_time' => $v['create_time'],
            'content' => $v['content'],
            'status' => $v['status'], // 添加 status 字段
            'reason' => $v['reason'], // 添加 status 字段
        ];
    }

    return $this->fetch('', [
        'record_list' => $recordList,
        'record' => $record
    ]);
}


public function changeStatus()
{
    $id = input('post.id');
    $status = input('post.status');

    if (Db::name('list')->where('id', $id)->update(['status' => $status])) {
        return json(['code' => 1, 'msg' => '状态更新成功']);
    } else {
        return json(['code' => 0, 'msg' => '状态更新失败']);
    }
}
    public function import()
    {
        require ROOT_PATH . '/public/extend/classes/PHPExcel/IOFactory.php';
        $excel = \PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);
        $data = $excel->getSheet(0)->toArray();
        unset($data[0]);
        if($data){
            $insert = [];
            $date = date('Y-m-d H:i:s');
            $qi = $this->qi;
            foreach($data as $v){
                $insert[] = [
                    'name' => $v[0],
                    'xzwcl' => $v[1] ? $v[1] : 0,
                    'xzgq' => isset($qi[$v[2]]) ? $qi[$v[2]] : 0,
                    'xswcl' => $v[3] ? $v[3] : 0,
                    'xsgq' => isset($qi[$v[4]]) ? $qi[$v[4]] : 0,
                    'create_time' => $date,
                    'type' => 1
                ];
            }
            if($insert){
                Db::name('tbb')->where('type',1)->update([
                    'type' => 0
                ]);
                Db::name('tbb')->insertAll($insert);
            }
        }
        return json([
            'error' => 0,
            'message' => '导入成功'
        ]);
    }

    public function edit()
    {
        $id = input('id');
        $record = Db::name('tbb')->where('id',$id)->find();
        return $this->fetch('',[
            'record' => $record
        ]);
    }
    
    
    public function rejectStatus()
{
    $id = input('post.id');
    $status = input('post.status');
    $reason = input('post.reason');

    if (Db::name('list')->where('id', $id)->update(['status' => $status, 'reason' => $reason])) {
        return json(['code' => 1, 'msg' => '状态更新成功']);
    } else {
        return json(['code' => 0, 'msg' => '状态更新失败']);
    }
}
    public function update()
    {
        $data            = $this->request->post();
        $validate_result = $this->validate($data, 'Tbb');
        if ($validate_result !== true) {
            $this->error($validate_result);
        } else {
           $id = $data['id'];
           unset($data['id']);
           Db::name('tbb')->where('id',$id)->update($data);
           $this->success('更新成功');
        }
    }
}
